<?php

global $wpss_surveys_table;
$wpss_url = WP_PLUGIN_URL . '/wordpress-simple-survey/';
$wpss_admin_url = admin_url() . 'admin.php?page=wpss-options';

if (isset($_GET['action']))
{
	if ($_GET['action'] == 'edit')
	{
		global $wpss_surveys_table;
		$currQuizID = $_GET['id'];
		global $wpdb;

		$q = $wpdb->get_row("SELECT * FROM $wpss_surveys_table WHERE surveyID = '$currQuizID'", ARRAY_A);
		//to do: check the returned row

		if(count($q) != 0)
		{
			$quizTitle = $q['title'];
			$quizIntro = $q['intro'];
			$quizOutro = $q['outro'];
			$numQuestions = $q['numQuestions'];
			$sendEmail = $q['sendEmail'];
			$questionsArray = explode("@",$q['questions']);
			for ($n = 0; $n < $numQuestions; $n++)
				$questionsArray[$n] = $questionsArray[$n];
		}
		
		require 'survey_options.php';
	}
	else//delete
	{
		$quizID = $_GET['id'];
		global $wpdb;
		global $wpss_surveys_table;

		$res = $wpdb->query("DELETE FROM $wpss_surveys_table WHERE surveyID = '$quizID'");

		require 'surveysList.php';
	}
}
else if ($_POST['wpss_hidden'] == 'SurveysListForm')//working in main form
{
	$currQuizID = $_POST['wpss_QuizID'];

	$quizTitle = "";
	$quizIntro = "";
	$quizOutro = "";
	$sendEmail = "";

	$numQuestions = "1";
	$questionsArray = "";

	//Create the quiz in the DB
	global $wpdb;
	global $wpss_surveys_table;

	$res = $wpdb->get_row("SELECT * FROM $wpss_surveys_table WHERE surveyID = '$currQuizID'");

	if ($res == NULL)
	{
		$r1 = $wpdb->insert($wpss_surveys_table, array('surveyID' => $currQuizID, 'numQuestions'=>$numQuestions, 'title'=>$quizTitle,
			'intro'=>$quizIntro, 'outro'=>$quizOutro, 'sendEmail'=>$sendEmail, 'questions'=>$questionsArray));

        if ($r1 != false)
        {
            ?>
            <script type="text/javascript">
                jQuery(document).ready(function($){
                    $(".wpss_info").tooltip({position: "center right", opacity: 1.0});
                });
            </script>
            <?php

            require 'survey_options.php';
        }
        else
        {
            ?>
            <script type="text/javascript">
                alert("Inserting Error!");
            </script>
            <?php
        }

	}
	else
	{
		?>
		<script type="text/javascript">
			alert("The quest alreay exists!");
		</script>
		<?php
	}
}
else if($_POST['wpss_hidden'] == 'SurveyOptionsForm')//working in properties form, change questions num or Done
{
   //Form data sent, update database (adds slashes) w POST, then local variable
	$quizTitle = htmlentities($_POST['wpss_quizTitle'], ENT_QUOTES, 'UTF-8');
	$quizIntro = htmlentities($_POST['wpss_quizIntro'], ENT_QUOTES, 'UTF-8');
	$quizOutro = htmlentities($_POST['wpss_quizOutro'], ENT_QUOTES, 'UTF-8');
	$numQuestions = htmlentities($_POST['wpss_numQuestions'], ENT_QUOTES, 'UTF-8');
	$sendEmail = htmlentities($_POST['wpss_sendEmail'], ENT_QUOTES, 'UTF-8');
	$currQuizID = htmlentities($_POST['wpss_QuizID'], ENT_QUOTES, 'UTF-8');


	/* Save/Grab Questions */
	$questionsArray = array();
	for ($n = 0; $n < $numQuestions; $n++) {
		$thisQuest = "wpss_questions_" . $n;
		$questionsArray[$n] = $_POST[$thisQuest];
	}

//save to db
	$arr = implode("@", $questionsArray);
	global $wpdb;
	global $wpss_surveys_table;
/* 	$result = $wpdb->update($wpss_surveys_table, array('numQuestions'=>(string)$numQuestions, 'title'=>(string)$quizTitle,
		'intro'=>(string)$quizIntro, 'outro'=>(string)$quizOutro, 'sendEmail'=>(string)$sendEmail, 'questions'=>$arr),
			array('surveyID'=>'$currQuizID'), array('%s', '%s'), array('%s')); */
			
	$q = "UPDATE $wpss_surveys_table 
		  SET  numQuestions = '$numQuestions', title = '$quizTitle', intro = '$quizIntro', outro = '$quizOutro', sendEmail = '$sendEmail', questions = '$arr'
		  WHERE surveyID = '$currQuizID'";
	$result = $wpdb->query($q);

	if (!empty($_POST['doneBtn']))
		require_once 'surveysList.php';
	else//num qyestions changed
		require_once 'survey_options.php';

   ?>
		<div class="updated"><p><strong><?php _e('Options saved.'); ?></strong></p></div>
   <?php
}
else//the form is still not submitted
{
	require 'surveysList.php';
}

?>
